/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import excepciones.UsuarioException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.apache.commons.codec.binary.Hex;

/**
 *
 * @author diego
 */
public class Usuario {
     private Integer id;
     private String userName;
     private String password;
     private String nivel;
     private boolean estado;

    public Usuario() {
    }

    public Usuario(String userName, String password, String nivel) throws UsuarioException, NoSuchAlgorithmException{       
        MessageDigest md;
        byte[] mb;
        
        md= MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        mb = md.digest();
        
        this.userName = userName;
        this.password = Arrays.toString(Hex.encodeHex(mb));
        this.nivel = nivel;
        this.estado = true;
        
        Complejo.persistencia.insert(this);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNivel() {
        return nivel;
    }

    public void setNivel(String nivel) {
        this.nivel = nivel;
    }

    public boolean isEstado() {
        return estado;
    }
    
    public boolean getEstado() {
        return estado;
    }


    public void setEstado(boolean estado) {
        this.estado = estado;
    }

    public boolean validarPass(String unPassword) throws UsuarioException, NoSuchAlgorithmException{
        boolean var = false;
        MessageDigest md;
        byte[] mb;
        
        md= MessageDigest.getInstance("MD5");
        md.update(unPassword.getBytes());
        mb = md.digest();
        
        String unPasswordEncriptado  = Arrays.toString(Hex.encodeHex(mb)); 
        
        if(this.getPassword().equals(unPasswordEncriptado)){
            var = true;
        }else{
            throw new UsuarioException("Contraseña incorrecta. Verifique",1);
        }
        
        return var;
        
    }
    
    public void desactivar(){
        this.estado = false;
    }
    
    public void activar(){
        this.estado = true;
    }
    
     @Override
    public String toString(){
        return this.userName;
    }
    
    
}
